<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="优惠劵名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="发行状态">
				<el-select v-model="dataForm.status" placeholder="发行状态">
					<el-option label="全部" value="" ></el-option>
					<el-option label="暂停发行" value="0" ></el-option>
					<el-option label="发行中" value="1" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="审核状态">
				<el-select v-model="dataForm.check" placeholder="审核状态">
					<el-option label="全部" value="" ></el-option>
					<el-option label="审核不通过" value="0" ></el-option>
					<el-option label="审核通过" value="1" ></el-option>
          <el-option label="审核中" value="3" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="发放方式">
				<el-select v-model="dataForm.issue" placeholder="发放方式">
					<el-option label="全部" value="" ></el-option>
					<el-option label="购劵中心" value="0" ></el-option>
					<el-option label="消费赠送" value="1" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('shoppingcoupon:shoppingstorecoupon:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('shoppingcoupon:shoppingstorecoupon:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <el-table-column
        prop="storeUserName"
        header-align="center"
        align="center"
        label="商家账号">
      </el-table-column>
      <el-table-column
        prop="storeTrueName"
        header-align="center"
        align="center"
        label="商家名称">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        label="优惠劵名称">
      </el-table-column>
      <el-table-column
        prop="fullMoney"
        header-align="center"
        align="center"
        label="满减">
        <template slot-scope="scope">
          <span>满{{scope.row.fullMoney}}减{{scope.row.money}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="money"
        header-align="center"
        align="center"
        label="能够减多少金额">
      </el-table-column> -->
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="结束时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="发行状态">
        <template slot-scope="scope">
          <span>{{scope.row.status === 0 ? '暂停发行' : '发行中'}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column> -->
      <!-- <el-table-column
        prop="describe"
        header-align="center"
        align="center"
        label="优惠劵描述">
      </el-table-column> -->
      <el-table-column
        prop="issue"
        header-align="center"
        align="center"
        label="发放方式">
        <template slot-scope="scope">
          <el-tag type="info">{{scope.row.issue === 0 ? '购劵中心' : '消费赠送'}}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="fullGet"
        header-align="center"
        align="center"
        label="当优惠劵发放方式为1，消费满足此金额才发放">
      </el-table-column> -->
      <el-table-column
        prop="issueAmount"
        header-align="center"
        align="center"
        label="发行量">
      </el-table-column>
      <!-- <el-table-column
        prop="residualAmount"
        header-align="center"
        align="center"
        label="剩余数量">
      </el-table-column>
      <el-table-column
        prop="getAmount"
        header-align="center"
        align="center"
        label="商家优惠劵被已领数量">
      </el-table-column>
      <el-table-column
        prop="useAmount"
        header-align="center"
        align="center"
        label="商家优惠劵被已使用数量">
      </el-table-column> -->
      <el-table-column
        prop="check"
        header-align="center"
        align="center"
        label="审核状态">
        <template slot-scope="scope">
          <el-button size="small" type="warning" icon="check" @click="storeCoupon(scope.row.id, 1)" v-if="scope.row.check === 3">通过</el-button>
          <el-button size="small" type="danger" icon="edit" @click="notStoreCoupon(scope.row.id, 0)" v-if="scope.row.check === 3">拒绝</el-button>
          <el-tag v-if="scope.row.check === 0">不通过</el-tag>
          <el-tag v-if="scope.row.check === 1">通过</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="checkNoPass"
        header-align="center"
        align="center"
        label="审核不通过原因">
      </el-table-column>
      <el-table-column
        prop="consumeMoney"
        header-align="center"
        align="center"
        label="发行此优惠劵消耗金额=发行量*0.1">
      </el-table-column>
      <el-table-column
        prop="deletestatus"
        header-align="center"
        align="center"
        label="删除标记">
      </el-table-column> -->
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="券购买价格">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">查看</el-button>
          <!-- <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './shoppingstorecoupon-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          status: '',
          check: '',
          issue: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/business/coupon/queryStoreCouponList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.key,
            'check': this.dataForm.check,
            'status': this.dataForm.status,
            'issue': this.dataForm.issue
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/shoppingcoupon/shoppingstorecoupon/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      storeCoupon (id, status) {
        this.$confirm(`确定对该用户进行审核通过操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/coupon/toExamineCouponById'),
            method: 'post',
            data: this.$http.adornData({id: id, check: status}, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
              this.getDataList()
            }
          })
        })
      },
      notStoreCoupon (id, status) {
        this.$prompt('请输入不通过原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({value}) => {
          this.$http({
            url: this.$http.adornUrl('/business/coupon/toExamineCouponById'),
            method: 'post',
            data: this.$http.adornData({id: id, checkNoPass: value, check: status}, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
              this.getDataList()
            }
          })
        })
      }
    }
  }
</script>
